package com.lebang.service.sys;

import com.lebang.common.utils.Base64;
import com.lebang.domain.AdBackLog;
import com.lebang.entity.RequestHeaders;
import com.lebang.example.AdBackLogExample;
import com.lebang.mapper.LbbMemberMapper;
import com.lebang.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

/**
 * @Author: hwq
 * @Date: 2020/12/7 11:39
 * @Version 1.0
 */
@Service
public class AdBackLogService extends BaseService<AdBackLog,Integer, AdBackLogExample> {
//    @Autowired
//    private KafkaTemplate<AdBackLog,AdBackLog> kafkaTemplate;
    public static byte[] a(byte[] var0) {
        if (var0 == null) {
            return null;
        } else {
            int var1 = var0.length;
            byte var2 = 18;

            for(int var3 = 0; var3 < var1; ++var3) {
                var0[var3] ^= var2;
            }

            return var0;
        }
    }
    @Autowired
    private LbbMemberMapper memberMapper;
    public void add(RequestHeaders requestHeaders, String sdkKey, String adId,String json) {
        LocalDateTime dateTime=LocalDateTime.now();
        json=json.replace("\r", "").replace("\n", "");
        byte[] decode = Base64.decode(json);
        decode=a(decode);
        json= new String(decode);
        AdBackLog record = new AdBackLog();
        record.setCreateTime(dateTime);
        record.setModifyTime(dateTime);
        record.setHasDeleted(0);
        record.setJson(json);
        record.setIpAddress(requestHeaders.getIpAddr());
        record.setImei(requestHeaders.getImei());
        record.setAppStore(requestHeaders.getAppStore());
        record.setVersion(requestHeaders.getVersion());
        record.setMemberId(requestHeaders.getUserid());
        record.setSdkKey(sdkKey);
        record.setAdId(adId);
        LocalDate date = memberMapper.selectCreaTimeByMemberId(requestHeaders.getUserid()).toLocalDate();
        if(date!=null){
            if (date.equals(dateTime.toLocalDate())) {
                record.setIsNewMember(1);
            } else {
                record.setIsNewMember((int) dateTime.toLocalDate().until(date, ChronoUnit.DAYS));
            }
        }else {
            record.setIsNewMember(0);
        }

        this.insertModel(record);
        //kafkaTemplate.send("ad_back_log",record);
    }
}
